package com.cn.demo4.service.impl;

import com.cn.demo4.dao.CompanyDao;
import com.cn.demo4.dao.impl.CompanyDaoImpl;
import com.cn.demo4.model.CompanyModel;
import com.cn.demo4.service.ICompanyService;
import com.cn.demo4.util.Tool;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务逻辑层
 *
 * @author Administrator
 */
public class CompanyServiceImpl implements ICompanyService {
//    @Override
//    public List<CompanyModel> findAll(CompanyModel companyModel) {
//        CompanyDao companyDao = new CompanyDaoImpl();
//        return companyDao.findAll(companyModel);
//    }



    @Override
    public CompanyModel findById(Integer id) {
        CompanyDao companyDao = new CompanyDaoImpl();
        return companyDao.findById(id);
    }

    @Override
    public String update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyDaoImpl();
        int result = companyDao.update(companyModel);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("companyNickName");
            jedis.del("companyTag");
            jedis.del("companyConsultingCenter");
            jedis.del("companyRecordNumber");
            jedis.del("companyRecordMessage");
            jedis.del("companyOnline");
            jedis.del("companyContent");
            jedis.del("companyEmailImg");
            jedis.del("companyEmail");
            jedis.del("companyPhoneImg");
            jedis.del("companyPhone");
            jedis.del("companyAddress");
            jedis.del("companyAddressImg");
            jedis.del("companyName");
            jedis.del("companyLogo");
            jedis.del("companyQrCodeIndexTitle");
            jedis.del("companyQrCodeIndex");
            jedis.del("companyQrCodeWeiXinTitle");
            jedis.del("companyQrCodeWeiXin");
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        CompanyDao companyDao = new CompanyDaoImpl();
        int result = companyDao.del(id);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("companyNickName");
            jedis.del("companyTag");
            jedis.del("companyConsultingCenter");
            jedis.del("companyRecordNumber");
            jedis.del("companyRecordMessage");
            jedis.del("companyOnline");
            jedis.del("companyContent");
            jedis.del("companyEmailImg");
            jedis.del("companyEmail");
            jedis.del("companyPhoneImg");
            jedis.del("companyPhone");
            jedis.del("companyAddress");
            jedis.del("companyAddressImg");
            jedis.del("companyName");
            jedis.del("companyLogo");
            jedis.del("companyQrCodeIndexTitle");
            jedis.del("companyQrCodeIndex");
            jedis.del("companyQrCodeWeiXinTitle");
            jedis.del("companyQrCodeWeiXin");
        }
        return result;
    }

    @Override
    public CompanyModel getCompanyModelListForHome() {
        CompanyModel companyModel = new CompanyModel();
        //判断当前缓存中是否又数据
        Jedis jedis = new Jedis();
        Boolean exists=jedis.exists("companyId");
        if(exists){
            //取缓存
            companyModel.setQrCodeWeiXin(jedis.get("companyQrCodeWeiXin"));
            companyModel.setQrCodeWeiXinTitle(jedis.get("companyQrCodeWeiXinTitle"));
            companyModel.setQrCodeIndex(jedis.get("companyQrCodeIndex"));
            companyModel.setQrCodeIndexTitle(jedis.get("companyQrCodeIndexTitle"));
            companyModel.setLogo(jedis.get("companyLogo"));
            companyModel.setName(jedis.get("companyName"));
            companyModel.setAddress(jedis.get("companyAddress"));
            companyModel.setAddressImg(jedis.get("companyAddressImg"));
            companyModel.setPhone(jedis.get("companyPhone"));
            companyModel.setPhoneImg(jedis.get("companyPhoneImg"));
            companyModel.setEmail(jedis.get("companyEmail"));
            companyModel.setEmailImg(jedis.get("companyEmailImg"));
            companyModel.setContent(jedis.get("companyContent"));
            companyModel.setOnline(jedis.get("companyOnline"));
            companyModel.setRecordNumber(jedis.get("companyRecordMessage"));
            companyModel.setRecordNumber(jedis.get("companyRecordNumber"));
            companyModel.setConsultingCenter(jedis.get("companyConsultingCenter"));
            companyModel.setTag(jedis.get("companyTag"));
            companyModel.setNickName(jedis.get("companyNickName"));
        }else{
            //想要缓存中又数据，就要先将数据存入缓存中
            CompanyDao companyDao = new CompanyDaoImpl();
            companyModel= companyDao.getCompanyModelListForHome();
            //创建jedis对象
            jedis.set("companyId",Integer.toString(companyModel.getId()));
            jedis.set("companyQrCodeWeiXin",companyModel.getQrCodeWeiXin());
            jedis.set("companyQrCodeIndex",companyModel.getQrCodeIndex());
            jedis.set("companyQrCodeWeiXinTitle",companyModel.getQrCodeWeiXinTitle());
            jedis.set("companyQrCodeIndexTitle",companyModel.getQrCodeIndexTitle());
            jedis.set("companyLogo",companyModel.getLogo());
            jedis.set("companyName",companyModel.getName());
            jedis.set("companyAddress",companyModel.getAddress());
            jedis.set("companyAddressImg",companyModel.getAddressImg());
            jedis.set("companyPhone",companyModel.getPhone());
            jedis.set("companyPhoneImg",companyModel.getPhoneImg());
            jedis.set("companyEmail",companyModel.getEmail());
            jedis.set("companyEmailImg",companyModel.getEmailImg());
            jedis.set("companyContent",companyModel.getContent());
            jedis.set("companyOnline",companyModel.getOnline());
            jedis.set("companyRecordMessage",companyModel.getRecordMessage());
            jedis.set("companyRecordNumber",companyModel.getRecordNumber());
            jedis.set("companyConsultingCenter",companyModel.getConsultingCenter());
            jedis.set("companyTag",companyModel.getTag());
            jedis.set("companyNickName",companyModel.getNickName());

        }
        return companyModel;

    }

    @Override
    public int getCount(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyDaoImpl();
        return companyDao.getCount(companyModel);
    }

    @Override
    public CompanyModel findAll(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyDaoImpl();
        return companyDao.findAll(companyModel);
    }

    /**
     * 服务逻辑层，调用数据实现层
     * 1.判断缓存里面是否又护具
     * 2.如果又，则将数据去除并封装到CompanyModle
     * 3.如果没有，则直接冲数据库中取数据
     *
     * @return
     */
//    @Override
//    public List<CompanyModel> getCompanyModelListForHome() {
//        List<CompanyModel> list = new ArrayList<>();
//        Jedis jedis = new Jedis();
//        CompanyDao companyDao = new CompanyDaoImpl();
//        Boolean exists = jedis.exists("companyId");
//        if(exists){
//            List<String> companyIdList = jedis.lrange("companyId",0,-1);
//            for(int i = 0; i < companyIdList.size(); i++){
//                CompanyModel companyModel = new CompanyModel();
//                companyModel.setId(Tool.stringToInt(jedis.lindex("companyId",i)));
//                companyModel.setStatus(Tool.stringToInt(jedis.lindex("companyStatus",i)));
//                companyModel.setQrCodeWeiXin(jedis.lindex("companyModelQrCodeWeiXin",i));
//                companyModel.setQrCodeIndex(jedis.lindex("companyModelQrCodeIndex",i));
//                companyModel.setLogo(jedis.lindex("companyLogo",i));
//                companyModel.setQrCodeIndexTitle(jedis.lindex("companyQrCodeIndexTitle",i));
//                companyModel.setQrCodeWeiXinTitle(jedis.lindex("companyQrCodeWeiXinTitle",i));
//                companyModel.setAddress(jedis.lindex("companyAddress",i));
//                companyModel.setAddressImg(jedis.lindex("companyAddressImg",i));
//                companyModel.setName(jedis.lindex("companyName",i));
//                companyModel.setPhone(jedis.lindex("companyPhone",i));
//                companyModel.setPhoneImg(jedis.lindex("companyPhoneImg",i));
//                companyModel.setEmail(jedis.lindex("companyEmail",i));
//                companyModel.setEmailImg(jedis.lindex("companyEmailImg",i));
//                companyModel.setContent(jedis.lindex("companyContent",i));
//                companyModel.setOnline(jedis.lindex("companyOnline",i));
//                companyModel.setRecordMessage(jedis.lindex("companyRecordMessage",i));
//                companyModel.setRecordNumber(jedis.lindex("companyRecordNumber",i));
//                companyModel.setConsultingCenter(jedis.lindex("companyConsultingCenter",i));
//                companyModel.setTag(jedis.lindex("companyTag",i));
//                companyModel.setNickName(jedis.lindex("companyNickName",i));
//                list.add(companyModel);
//            }
//        }else {
//            //将数据存入redis中
//            list = companyDao.getCompanyModelListForHome();
//            for(int i = 0; i < list.size(); i++){
//                jedis.rpush("companyId",Integer.toString(list.get(i).getId()));
//                jedis.rpush("companyStatus",Integer.toString(list.get(i).getStatus()));
//                jedis.rpush("companyQrCodeWeiXin",list.get(i).getQrCodeWeiXin());
//                jedis.rpush("companyQrCodeIndex",list.get(i).getQrCodeIndex());
//                jedis.rpush("companyQrCodeWeiXinTitle",list.get(i).getQrCodeWeiXinTitle());
//                jedis.rpush("companyQrCodeIndexTitle",list.get(i).getQrCodeIndexTitle());
//                jedis.rpush("companyLogo",list.get(i).getLogo());
//                jedis.rpush("companyName",list.get(i).getName());
//                jedis.rpush("companyAddress",list.get(i).getAddress());
//                jedis.rpush("companyAddressImg",list.get(i).getAddressImg());
//                jedis.rpush("companyPhone",list.get(i).getPhone());
//                jedis.rpush("companyPhoneImg",list.get(i).getPhoneImg());
//                jedis.rpush("companyEmail",list.get(i).getEmail());
//                jedis.rpush("companyEmailImg",list.get(i).getEmailImg());
//                jedis.rpush("companyContent",list.get(i).getContent());
//                jedis.rpush("companyOnline",list.get(i).getOnline());
//                jedis.rpush("companyRecordMessage",list.get(i).getRecordMessage());
//                jedis.rpush("companyRecordNumber",list.get(i).getRecordNumber());
//                jedis.rpush("companyConsultingCenter",list.get(i).getConsultingCenter());
//                jedis.rpush("companyTag",list.get(i).getTag());
//                jedis.rpush("companyNickName",list.get(i).getNickName());
//            }
//        }
//        return list;
//    }
    @Override
    public String add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyDaoImpl();
        int result = companyDao.add(companyModel);
        if(result == 1) {
            Jedis jedis = new Jedis();
//            jedis.flushDB();
            jedis.del("companyId","companyStatus","companyModelQrCodeWeiXin",
                    "companyQrCodeIndex","companyQrCodeWeiXinTitle",
                    "companyQrCodeIndexTitle","companyLogo","companyName",
                    "companyAddress","companyAddressImg","companyPhone","companyPhoneImg"
            ,"companyEmail","companyEmailImg","companyContent","companyOnline","companyRecordMessage",
                    "companyRecordNumber","companyConsultingCenter","companyTag","companyNickName");

        }
        return result  == 1 ? "success" : "error";
    }
}
